package com.masterclient.app.businessmodule.vipmodule.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.eoner.managerlibrary.system.SystemConfigStorage;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.vipmodule.bean.Level;
import com.masterclient.app.businessmodule.vipmodule.card.GeneralCard;
import com.masterclient.app.businessmodule.vipmodule.card.IBaseCard;
import com.masterclient.app.businessmodule.vipmodule.card.OtherCard;
import com.masterclient.app.businessmodule.vipmodule.card.VipCard;
import com.masterclient.app.businessmodule.vipmodule.fragment.VipCenterFragment;
import com.masterclient.app.modules.bridge.BridgeActivity;
import com.masterclient.app.widget.recyclerhelp.AdapterMeasureHelper;

import java.util.ArrayList;
import java.util.List;


public class MemberAdapter extends RecyclerView.Adapter<IBaseCard> {

    private Context mContext;
    private List<Level> levelList;
    private int currentLevel;
    private VipCenterFragment fragment;
    private AdapterMeasureHelper mCardAdapterHelper = new AdapterMeasureHelper();

    public MemberAdapter(Context ctx) {
        this.mContext = ctx;
        levelList = new ArrayList<>();
    }

    public void setMemberList(List<Level> levelList, int currentLevel, VipCenterFragment fragment) {
        this.levelList = levelList;
        this.fragment = fragment;
        this.currentLevel = currentLevel;
        notifyDataSetChanged();
    }

    public Level getLevel(int postion) {
        return levelList.get(postion);
    }

    @Override
    public IBaseCard onCreateViewHolder(ViewGroup parent, int viewType) {

        if (1 == viewType) {// 普通会员
            return new GeneralCard(setCardView(parent, R.layout.card_genral), mContext);
        } else if (2 == viewType) {// vip
            return new VipCard(setCardView(parent, R.layout.card_vip), mContext);
        } else {// 银卡及以上会员
            return new OtherCard(setCardView(parent, R.layout.card_other), mContext);
        }
    }

    private View setCardView(ViewGroup parent, int layoutId) {
        View view = LayoutInflater.from(mContext).inflate(layoutId, parent, false);
        return view;
    }

    @Override
    public void onBindViewHolder(final IBaseCard holder, final int position) {
        int item = position;
        if (position == levelList.size() - 1)
            item = -1;
        mCardAdapterHelper.onCreateViewHolder(mContext, holder.itemView, item);
        Level level = levelList.get(position);
        int levelVal = level.getLevel();
        level.setCurrentLevel(currentLevel);
        holder.onBindViewHolder(level, position);
        holder.itemView.setOnClickListener(view -> {
            if (levelVal < 3) {
                startWebViewActivity("成长值", fragment.exp_h5_url);
            }
        });
        // 立即开通
        holder.itemView.findViewById(R.id.open_tv).setOnClickListener(v -> {
            if (currentLevel < levelVal) {
                if (levelVal < 4)// 会员介绍
                    startWebViewActivity("dimissTitle", SystemConfigStorage.getInstance().getMember_ship_url());
                else if (levelVal < 6)// 开通店主
                    startWebViewActivity("dimissTitle", fragment.shop_keeper_url);
                else startWebViewActivity("dimissTitle", fragment.be_dealer_url);// 成为经销商
            }
        });

    }

    @Override
    public int getItemViewType(int position) {
        return levelList.get(position).getLevel();
    }

    @Override
    public int getItemCount() {
        return null == levelList ? 0 : levelList.size();
    }

    /**
     * 跳转到H5
     *
     * @param title
     * @param url
     */
    private void startWebViewActivity(String title, String url) {
        if (TextUtils.isEmpty(url))
            return;
        Intent intent = new Intent(mContext, BridgeActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("url", url);
        bundle.putString("title", title);
        intent.putExtras(bundle);
        mContext.startActivity(intent);
    }
}
